#!/bin/bash
file=`dirname $0`/passwd.csv
dirbf=`dirname $0`/bf/
cd `dirname $0`
lookup()
{
	if [ ! -f "$file" ]
	then
		zenity --warning \
		--text="err!no file!"
		exit 0
	fi
	total=`cat $file | wc -l`
	if [ $total -eq 1 ]
	then
		zenity --warning \
		--text="no data ,please add!"
		exit 0	
	fi
	echo 'zenity --list\
		--title=platform\
		--column=count --column=name --column=zhanghao1 --column=passwd --column=url --column=note --column=date\'>tmp

	cnt=2
	#total=20
	while [ $cnt -le $total ]
	do
		#echo $cnt
		doc=`awk 'NR=='$cnt'' $file`

		tt0=${doc// /''} 
		tt1=${tt0//,,,,,,/,-,-,-,-,-,} 
		tt2=${tt1//,,,,,/,-,-,-,-,} 
		tt3=${tt2//,,,,/,-,-,-,} 
		tt4=${tt3//,,,/,-,-,} 
		tt5=${tt4//,,/,-,} 
		t1=${tt5//,/ } 
		#echo $t1
		if [ $cnt -eq  $total ] 
		then
			echo ' '${t1}>>tmp
		else
			echo ' '${t1}'\'>>tmp
		fi	
		cnt=$[$cnt+1]
	done
		
	while read line
	do
	$line &
	done < tmp

	rm tmp
	echo over---------------------	
}

if [ "$1" == "-l" ] 
then
	lookup
	exit 0
fi

if [ "$1" == "-r" ]
then
	sudo chown root:root $file
	sudo chmod 600 $file
	exit 0
fi

if [ "$1" == "-u" ] 
then
	sudo chown ${USER}:${USER} $file
	sudo chmod 666 $file
	exit 0
fi

if [ "$1" == "-k" ] 
then
	if [ ! -n "$2" ] 
	then
		zenity --error --text="od err!"
		exit 0
	fi
	sudo chown ${USER}:${USER} $file
	sudo chmod 666 $file
	tar -czvf - passwd.csv | openssl des3 -salt -k $2 -out passwd.csv.tar.gz
	sudo chown root:root $file
	sudo chmod 600 $file	
	exit 0
fi

if [ "$1" == "-uk" ] 
then	
	if [ ! -n "$2" ]
	then
		zenity --error --text="od err!"
		exit 0
	fi
	sudo chown ${USER}:${USER} $file
	sudo chmod 666 $file
	if [ ! -d  "./bf" ]
	then
		mkdir bf	
	fi
	tar -czvf - passwd.csv | openssl des3 -salt -k $2 -out ./bf/passwd.csv.tar.gz
	
	dd if=passwd.csv.tar.gz |openssl des3 -d -k $2|tar zxf -
	sudo chown root:root $file
	sudo chmod 600 $file
	exit 0
fi


if [ "$1" == "-b" ] 
then	
	if [ ! -n "$2" ] 
	then
		zenity --error --text="od err!"
		exit 0
	fi
	sudo chown ${USER}:${USER} $file
	sudo chmod 666 $file	
	if [ ! -d  "./bf" ]
	then
		mkdir bf	
	fi
	tar -czvf - passwd.csv | openssl des3 -salt -k $2 -out ./bf/passwd.csv.tar.gz
	sudo chown root:root $file
	sudo chmod 600 $file
	exit 0
fi





if [ ! -f "$file" ]
then

	getYN=`zenity --question --text="no data,are you new "`
	case $? in
         0)
			touch $file
			#sudo chown root:root $file
			#sudo chmod 600 $file
			echo "-,-,-,-,-,-,-">>$file
			echo "----------------------------"
		;;
         1)

                exit 0
                ;;
        -1)

                exit 0
                ;;
	esac

fi

new_passwd()
{

	zenity --forms --title="新建密码" \
		--text="添加新账户到密码管理" \
		--separator="," \
		--add-entry="账号平台" \
		--add-entry="账户名" \
		--add-entry="账号"  \
		--add-entry="登录密码" \
		--add-entry="网址"  \
		--add-entry="说明"  \
		--add-calendar="新建日期">>$file
}

new_passwd
